package com.hzlh.controller.pc;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzlh.mapstruct.TTalentsBaseMapper;
import com.hzlh.model.*;
import com.hzlh.service.TTalentsBaseService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * (TTalentsBase)表控制层
 *
 * @author makejava
 * @since 2021-06-06 15:18:32
 */
@RestController
@RequestMapping("/talentsBase")
public class TTalentsBaseController {
    /**
     * 服务对象
     */
    @Resource
    private TTalentsBaseService tTalentsBaseService;

    /**
     * 人才区域统计
     *
     * @return
     */
    @GetMapping("/areaCount")
    public Object areaCount(@RequestParam(value = "keyword", defaultValue = "", required = false) String keyword,
                            @RequestParam(value = "agencyGroup", defaultValue = "", required = false) List<String> agencyGroup,
                            @RequestParam(value = "remarkGroup", defaultValue = "", required = false) List<String> remarkGroup,
                            @RequestParam(value = "fieldGroup", defaultValue = "", required = false) List<String> fieldGroup) {
        List<Map<String, Object>> countryCount = tTalentsBaseService.selectCountryCount(remarkGroup, agencyGroup, fieldGroup, keyword);
        List<Map<String, Object>> provinceCount = tTalentsBaseService.selectProvinceCount(remarkGroup, agencyGroup, fieldGroup, keyword);
        List<Map<String, Object>> cityCount = tTalentsBaseService.selectCityCount(remarkGroup, agencyGroup, fieldGroup, keyword);
        ArrayList<List<Map<String, Object>>> lists = new ArrayList<>();
        lists.add(countryCount);
        lists.add(provinceCount);
        lists.add(cityCount);
        return lists;
    }

    /**
     * 查询人才标签、论文发布领域、就职机构列表
     *
     * @return
     */
    @GetMapping("/getLabelList")
    public Object getLabelList(TTalentsBase tTalentsBase) {
        return tTalentsBaseService.getLabelList(tTalentsBase);
    }

    /**
     * 查询人才列表
     *
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/getList")
    public PageInfo getList(TTalentsBase tTalentsBase,
                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        Page<Object> page = PageHelper.startPage(pageIndex, pageSize);
        List<TTalentsBase> tTalentsBases = tTalentsBaseService.selectList(tTalentsBase);
        long total = page.getTotal();
        List<TTalentsBaseListDto> tTalentsBaseListDtos = TTalentsBaseMapper.INSTANCE.T_TALENTS_BASE_LIST_DTOS(tTalentsBases);
        tTalentsBaseListDtos.stream().forEach(v -> {
            List<Map<String, Object>> maps = new ArrayList<>();
            try {
                HashMap map = JSON.parseObject(v.getTalentsRel(), HashMap.class);
                Map<String, List<Object>> a = (Map<String, List<Object>>) map.get("similar_talent");
                List<Object> talentsList = a.get("相似人才");
                for (int i = 0; i < talentsList.size(); i++) {
                    HashMap<String, Object> hashMap = new HashMap<>();
                    hashMap.put("similarTalents", talentsList.get(i));
                    hashMap.put("similarity", a.get("相似度").get(i));
                    hashMap.put("thesisField", a.get("论文领域").get(i));
                    hashMap.put("paperTitle", a.get("论文标题").get(i));
                    maps.add(hashMap);
                }
                v.setTalentsRelFormat(maps);
            } catch (Exception e) {
                System.out.println("json解析异常");;
            }
        });
        PageInfo<TTalentsBaseListDto> pageInfo = new PageInfo<>(tTalentsBaseListDtos);
        pageInfo.setTotal(total);
        return pageInfo;
    }

    /**
     * 查询相似人才列表
     *
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/getLikeList")
    public PageInfo getLikeList(TTalentsBase tTalentsBase,
                                @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                                @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TTalentsBase> tTalentsBases = tTalentsBaseService.selectList(tTalentsBase);
        List<TTalentsBaseLikeListDto> likeListDtos = TTalentsBaseMapper.INSTANCE.T_TALENTS_BASE_LIKE_LIST_DTOS(tTalentsBases);
        return new PageInfo<>(likeListDtos);
    }

    /**
     * 人才分析
     *
     * @return
     */
    @GetMapping("/talentAnalysis")
    public Object talentAnalysis(TTalentsBase tTalentsBase) {
        return tTalentsBaseService.talentAnalysis(tTalentsBase);
    }
}
